package servidor;

import java.io.BufferedReader;
import java.io.FileReader;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
 * 
 * @author Douglas Borges
 * 
 */
public abstract class Server implements InterfaceServicos {

  public static String diretorioPadrao;
  public static String servidor;
  public static String ip;
  public static String porta;


  public Server() {
    super();
  }


  public static void main(String args[]) {
    try {
      InterfaceServicos it = new ServidorServicos();

      String caminho = Server.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
      // caminho = caminho.substring(1, caminho.lastIndexOf("/") + 1)
      // .replace("/", "\\");

      caminho = caminho.substring(0, caminho.lastIndexOf("/") + 1);

      BufferedReader br = new BufferedReader(new FileReader(String.format("%sProp.ini", caminho)));
      servidor = br.readLine().replace("Servidor=", "");
      ip = br.readLine().replace("IP=", "");
      porta = br.readLine().replace("Porta=", "");
      diretorioPadrao = br.readLine().replace("diretorioPadrao=", "");

      br.close();

      InterfaceServicos stub = (InterfaceServicos) UnicastRemoteObject.exportObject(it, 0);
      Registry registry = LocateRegistry.createRegistry(Integer.parseInt(porta));
      System.setProperty("java.rmi.server.hostname", ip);

      registry.rebind(servidor, stub);
      Naming.rebind(servidor, stub);

    }
    catch (Exception e) {
      System.out.println("ERRO: " + e);
    }
    System.out.println("Servidor Iniciado!\nAguardando Arquivos para Compilar...");

  }
}
